package com.example.demo.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.entity.AccessoriesChannels;
import com.example.demo.entity.AccessoriesChannels2;
import com.example.demo.entity.SysDept;
import com.example.demo.entity.SysUser;
import com.example.demo.form.AccessoriesChannelsFrom;
import com.example.demo.service.AccessoriesChannels2Service;
import com.example.demo.service.AccessoriesChannelsService;
import com.example.demo.service.SysDeptService;
import com.example.demo.service.SysUserService;

@RestController
@RequestMapping(value = "/AccessoriesChannels")
public class AccessoriesChannelsController {
	@Autowired
	private AccessoriesChannelsService service1;
	@Autowired
	private AccessoriesChannels2Service service2;
	@Autowired
	private SysDeptService deptDAO;
	@Autowired
	private SysUserService sysUserDAO;
	
	private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");

	@RequestMapping(value = "/save")
	private Object save(AccessoriesChannelsFrom Form) throws ParseException {
		Integer accid1 = Form.getId();
		Date d = new Date();
		Date d1 = dateFormat1.parse("1999-01-01");
		AccessoriesChannels acc = new AccessoriesChannels();
		AccessoriesChannels Acc = new AccessoriesChannels();
		AccessoriesChannels2 acc2 = new AccessoriesChannels2();
		AccessoriesChannels2 Acc2 = new AccessoriesChannels2();
		List<AccessoriesChannels> accAll = service1.findAll();
		if (accid1 == null) {
			if (accAll.size()>0) {
				for (AccessoriesChannels accessoriesChannels : accAll) {
					if (Form.getName().equals(accessoriesChannels.getName())) {
						acc=accessoriesChannels;					
					} else {
						Acc.setName(Form.getName());
						acc = service1.save(Acc);
					}
				}
			} else {
				Acc.setName(Form.getName());
				acc = service1.save(Acc);
			}
		} else {
			
			
			
			
			acc = service1.findById(accid1);
			List<AccessoriesChannels2> list = service2.findByAccessoriesChannels(acc);
			for (AccessoriesChannels2 access : list) {
				if (d1.compareTo(access.getBeginDate()) == -1) {
					d1 = access.getBeginDate();
					Acc2 = access;
				}
			}
			Acc2.setEndDate(d);
			service2.save(Acc2);
		}
		BeanUtils.copyProperties(Form, acc2, "id", "name");
		acc2.setAccessoriesChannels(acc);
		acc2.setBeginDate(d);
		service2.save(acc2);
		return new AjaxResult("数据保存成功");
	}

	@RequestMapping(value = "/list")
	private Object list() {
		return new AjaxResult(service1.findAll());
	}
	
	@RequestMapping(value = "/findbyId")
	private Object findbyId(Integer id) throws ParseException {
		List<AccessoriesChannels2> list = service2.findByAccessoriesChannelsId(id);
		Date d = dateFormat1.parse("1999-01-01");
		AccessoriesChannels2 acc = new AccessoriesChannels2();
		for (AccessoriesChannels2 y : list) {
			if (d.compareTo(y.getBeginDate()) == -1) {
				d = y.getBeginDate();
				acc = y;
			}
		}
		return new AjaxResult(acc);
	}
	@RequestMapping(value = "/findbyAccessoriesChannels2")
	private Object findbyAccessoriesChannels2(AccessoriesChannelsFrom From) throws ParseException {
		List<AccessoriesChannels2> list = service2.findByAccessoriesChannelsNameAndCategory(From.getName(),From.getCategory());
		Date d = dateFormat1.parse("1999-01-01");
		AccessoriesChannels2 acc = new AccessoriesChannels2();
		for (AccessoriesChannels2 y : list) {
			if (d.compareTo(y.getBeginDate()) == -1) {
				d = y.getBeginDate();
				acc = y;
			}
		}
		return new AjaxResult(acc);
	}
	//查询售后部所有人员
	@RequestMapping(value = "/findShouHou")
	public Object findShouHou() {
		List<SysDept> allDept = deptDAO.findAll();
		for (SysDept sysDept : allDept) {
			if(sysDept.getParentId()==null&&"售后部".equals(sysDept.getName())) {
				Integer nameId = sysDept.getId();
				List<SysUser> a = sysUserDAO.findByDeptId(nameId);
//				for (SysUser a2 : a) {s
//					if("售后顾问".equals(a2.getZhiwei())) {
//						
//					}
//				}
				return new AjaxResult(a);
			}
		}
		return new AjaxResult(false);
	}

}
